package com.syni.mdd.sjb.common.datatreaure.controller.protect.dataTreasure.login;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.sjb.common.component.utils.common.utils.RestTemplateUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.databankUser.MerchantsUserInfoBo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.databankUser.GetMerchantsUserInfoRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.DataBankLoginVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.dxshAppNews.SettingStatusVo;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppNewsPushService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserNewsSwitchService;
import com.syni.mdd.sjb.common.datatreaure.service.databank.DataBankUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/databank/user")
public class BankUserController {

    @Autowired
    DataBankUserService dataBankUserService;

    @Autowired
    DxshAppUserNewsSwitchService dxshAppUserNewsSwitchService;

    @Autowired
    DxshAppNewsPushService dxshAppNewsPushService;

    @PostMapping("/register")
    @ApiOperation(value = "觅东东数据宝App注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号"),
            @ApiImplicitParam(name = "verificationCode",value = "验证码"),
            @ApiImplicitParam(name = "password",value = "注册密码")})
    public Map<String,Object> register(@RequestBody @Validated DataBankLoginVo dataBankLoginVo){
        return dataBankUserService.register(dataBankLoginVo);
    }


    @PostMapping("/login")
    @ApiOperation(value = "觅东东数据宝App登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号"),
            @ApiImplicitParam(name = "verificationCode",value = "验证码"),
            @ApiImplicitParam(name = "password",value = "注册密码")})
    public Map<String,Object> login(@RequestBody DataBankLoginVo dataBankLoginVo){
        return dataBankUserService.login(dataBankLoginVo);
    }

    @PostMapping("/forget")
    @ApiOperation(value = "觅东东数据宝App忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号"),
            @ApiImplicitParam(name = "verificationCode",value = "验证码"),
            @ApiImplicitParam(name = "password",value = "登陆密码")})
    public Map<String,Object> forget(@RequestBody  DataBankLoginVo dataBankLoginVo){
        return dataBankUserService.forget(dataBankLoginVo);
    }

    @PostMapping("/getMerchantsUserInfo")
    @ApiOperation(value = "获取商户个人信息")
    @CheckUserIdentity
    public Map<String,Object> getMerchantsUserInfo(@RequestBody DataBankLoginVo dataBankLoginVo){
        MerchantsUserInfoBo merchantsUserInfoBo = dataBankUserService.getMerchantsUserInfo(dataBankLoginVo.getUserId());
        GetMerchantsUserInfoRpVo getMerchantsUserInfoRpVo = new GetMerchantsUserInfoRpVo();
        getMerchantsUserInfoRpVo.setMerchantsUserInfo(merchantsUserInfoBo);
        return ResultMapHelper.success(getMerchantsUserInfoRpVo, ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/getMerchantsNoReadNews")
    @ApiOperation(value = "获取商户的消息未读通知")
    @CheckUserIdentity
    public Map<String,Object> getMerchantsNoReadNews(@RequestBody DataBankLoginVo dataBankLoginVo){
        return dataBankUserService.getMerchantsNoReadNews(dataBankLoginVo);
    }

    @PostMapping("/getMerchantsAlreadyReadNews")
    @ApiOperation(value = "获取商户的消息已读通知")
    @CheckUserIdentity
    public Map<String,Object> getMerchantsAlreadyReadNews(@RequestBody DataBankLoginVo dataBankLoginVo){
        return dataBankUserService.getMerchantsAlreadyReadNews(dataBankLoginVo);
    }

    @PostMapping("/getMerchantsNews")
    @ApiOperation(value = "获取商户的消息全部通知")
    @CheckUserIdentity
    public Map<String,Object> getMerchantsNews(@RequestBody DataBankLoginVo dataBankLoginVo){
        return dataBankUserService.getMerchantsNews(dataBankLoginVo);
    }

    @PostMapping("/getMerchantsNewsByChannelId")
    @ApiOperation(value = "获取商户的消息全部通知")
    @CheckUserIdentity
    public Map<String,Object> getMerchantsNewsByChannelId(@RequestBody DataBankLoginVo dataBankLoginVo){
        return dataBankUserService.getMerchantsNewsByChannelId(dataBankLoginVo);
    }

    @PostMapping("/rightSlip")
    @ApiOperation(value = "右滑标记为已读")
    @CheckUserIdentity
    public Map<String,Object> rightSlip(@RequestBody DataBankLoginVo dataBankLoginVo){
        dataBankUserService.rightSlip(dataBankLoginVo.getNewId());
        return ResultMapHelper.success(new HashMap<>(),ResultMsg.HAND_SUCCESS);
    }

    @PostMapping("/rightSlideDelete")
    @ApiOperation(value = "右滑标记为删除")
    @CheckUserIdentity
    public Map<String,Object> rightSlideDelete(@RequestBody DataBankLoginVo dataBankLoginVo){
        dataBankUserService.rightSlideDelete(dataBankLoginVo.getNewId());
        return ResultMapHelper.success(new HashMap<>(),ResultMsg.HAND_SUCCESS);
    }

    @PostMapping("/markAllAsRead")
    @ApiOperation(value = "全部标记为已读")
    @CheckUserIdentity
    public Map<String,Object> markAllAsRead(@RequestBody DataBankLoginVo dataBankLoginVo){
        dataBankUserService.markAllAsRead(dataBankLoginVo.getUserId(),dataBankLoginVo.getMessageType(),dataBankLoginVo.getChannelId());
        return ResultMapHelper.success(new HashMap<>(),ResultMsg.HAND_SUCCESS);
    }

    @PostMapping("/getMyMessageSettings")
    @ApiOperation(value = "消息设置")
    @CheckUserIdentity
    public Map<String,Object> getMyMessageSettings(@RequestBody DataBankLoginVo dataBankLoginVo){
        return dxshAppUserNewsSwitchService.getMyMessageSettings(dataBankLoginVo.getUserId());
    }

    @PostMapping("/changeSettingStatus")
    @ApiOperation(value = "更改设置状态")
    @CheckUserIdentity
    public Map<String,Object> changeSettingStatus(@RequestBody SettingStatusVo settingStatusVo){
        return dxshAppUserNewsSwitchService.changeSettingStatus(settingStatusVo);
    }

    @PostMapping("/editPushTime")
    @ApiOperation(value = "编辑推送时间")
    @CheckUserIdentity
    public Map<String,Object> editPushTime(@RequestBody SettingStatusVo settingStatusVo){
        return dxshAppNewsPushService.editPushTime(settingStatusVo);
    }

    @PostMapping("/test")
    @ApiOperation(value = "编辑推送时间")
    public String test(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String params = "{\n" +
                "    \"nonce_str\":\"b4ddeb5804634758a8737b60d0c94b5a\",\n" +
                "    \"sign\":\"F7F6854BABA0CB94F0C32600782CA63982E2379DC0573389258FBBF100F878FC\",\n" +
                "    \"fee_type\":\"CNY\",\n" +
                "    \"mch_id\":\"1563829591\",\n" +
                "    \"cash_fee\":\"1\",\n" +
                "    \"total_fee\":\"1\",\n" +
                "    \"attach\":\"默认\",\n" +
                "    \"time_end\":\"20201010120534\",\n" +
                "    \"transaction_id\":\"4200000699202010107416488288\",\n" +
                "    \"bank_type\":\"OTHERS\",\n" +
                "    \"openid\":\"oBnK76tmmwnVHtLjjPi3SIRX5Ans\",\n" +
                "    \"out_trade_no\":\"764458561181843456\",\n" +
                "    \"appid\":\"wx4b0084c3c94f9abf\",\n" +
                "    \"trade_type\":\"APP\",\n" +
                "    \"result_code\":\"SUCCESS\",\n" +
                "    \"is_subscribe\":\"N\",\n" +
                "    \"return_code\":\"SUCCESS\"\n" +
                "}";
        Map<String,String> po = JSON.parseObject(params,Map.class);
        String externalURL = "http://dg.syni.com/MDD_API/commonTool/common/forwardingInterface";
        //params.put("notifyUrl",notifyUrl);
        RestTemplateUtils.forwardPost(externalURL,request,po);
        return "SUCCESS";
    }



}
